<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>Function接口简单代码结构 | 若愚湖畔</title><meta name="keywords" content="java"><meta name="author" content="albafica"><meta name="copyright" content="albafica"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Function 函数式接口函数式接口就是使用注解@FunctionalInterface标识，并且只包含一个抽象方法的接口是函数式接口。 函数式接口主要分为Supplier供给型函数、Consumer消费型函数、Runnable无参无返回型函数和Function有参有返回型函数。  Function可以看作转换型函数  函数说明Supplier供给型函数Supplier的表现形式为不接受参数、只">
<meta property="og:type" content="article">
<meta property="og:title" content="Function接口简单代码结构">
<meta property="og:url" content="https://albafica.gitee.io/2022/05/07/functionalinterface/index.html">
<meta property="og:site_name" content="若愚湖畔">
<meta property="og:description" content="Function 函数式接口函数式接口就是使用注解@FunctionalInterface标识，并且只包含一个抽象方法的接口是函数式接口。 函数式接口主要分为Supplier供给型函数、Consumer消费型函数、Runnable无参无返回型函数和Function有参有返回型函数。  Function可以看作转换型函数  函数说明Supplier供给型函数Supplier的表现形式为不接受参数、只">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="">
<meta property="article:published_time" content="2022-05-07T10:18:48.000Z">
<meta property="article:modified_time" content="2022-05-07T12:06:25.237Z">
<meta property="article:author" content="albafica">
<meta property="article:tag" content="java">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content=""><link rel="shortcut icon" href="/hexoblog/img/favicon.png"><link rel="canonical" href="https://albafica.gitee.io/2022/05/07/functionalinterface/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="stylesheet" href="/hexoblog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/hexoblog/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":200},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Function接口简单代码结构',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-05-07 20:06:25'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.1.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://s1.ax1x.com/2022/04/23/Lhpz3F.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data is-center"><div class="data-item"><a href="/hexoblog/archives/"><div class="headline">文章</div><div class="length-num">11</div></a></div><div class="data-item"><a href="/hexoblog/tags/"><div class="headline">标签</div><div class="length-num">6</div></a></div><div class="data-item"><a href="/hexoblog/categories/"><div class="headline">分类</div><div class="length-num">2</div></a></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/hexoblog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 目录</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/hexoblog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/hexoblog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/hexoblog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/hexoblog/">若愚湖畔</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/hexoblog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 目录</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/hexoblog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/hexoblog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/hexoblog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Function接口简单代码结构</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-05-07T10:18:48.000Z" title="发表于 2022-05-07 18:18:48">2022-05-07</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-05-07T12:06:25.237Z" title="更新于 2022-05-07 20:06:25">2022-05-07</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">1.7k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>6分钟</span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="Function-函数式接口"><a href="#Function-函数式接口" class="headerlink" title="Function 函数式接口"></a>Function 函数式接口</h1><p>函数式接口就是使用注解<code>@FunctionalInterface</code>标识，并且只包含一个抽象方法的接口是函数式接口。</p>
<p>函数式接口主要分为<code>Supplier供给型函数</code>、<code>Consumer消费型函数</code>、<code>Runnable无参无返回型函数</code>和<code>Function有参有返回型函数</code>。</p>
<blockquote>
<p>Function可以看作转换型函数</p>
</blockquote>
<h2 id="函数说明"><a href="#函数说明" class="headerlink" title="函数说明"></a>函数说明</h2><h3 id="Supplier供给型函数"><a href="#Supplier供给型函数" class="headerlink" title="Supplier供给型函数"></a>Supplier供给型函数</h3><p>Supplier的表现形式为不接受参数、只返回数据</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">代表结果的提供者。</span></span><br><span class="line"><span class="comment">不要求每次调用供应商时都返回新的或不同的结果。</span></span><br><span class="line"><span class="comment">这是一个功能接口，其功能方法是get() 。</span></span><br><span class="line"><span class="comment">自：</span></span><br><span class="line"><span class="comment">1.8</span></span><br><span class="line"><span class="comment">类型形参：</span></span><br><span class="line"><span class="comment">&lt; T &gt; – 该供应商提供的结果类型</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@FunctionalInterface</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title class_">Supplier</span>&lt;T&gt; &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">    * Gets a result.</span></span><br><span class="line"><span class="comment">    * 返回值: result</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    T <span class="title function_">get</span><span class="params">()</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="Consumer消费型函数"><a href="#Consumer消费型函数" class="headerlink" title="Consumer消费型函数"></a>Consumer消费型函数</h3><p><code>Consumer</code>消费型函数和<code>Supplier</code>刚好相反。<code>Consumer</code>接收一个参数，没有返回值</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">表示接受单个输入参数且不返回结果的操作。与大多数其他功能接口不同， Consumer预计将通过副作用进行操作。</span></span><br><span class="line"><span class="comment">这是一个功能接口，其功能方法是accept(Object) 。</span></span><br><span class="line"><span class="comment">自：</span></span><br><span class="line"><span class="comment">1.8</span></span><br><span class="line"><span class="comment">类型形参：</span></span><br><span class="line"><span class="comment">&lt; T &gt; – 操作的输入类型</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@FunctionalInterface</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title class_">Consumer</span>&lt;T&gt; &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">    对给定参数执行此操作。</span></span><br><span class="line"><span class="comment">    参形：</span></span><br><span class="line"><span class="comment">    t - 输入参数</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">accept</span><span class="params">(T t)</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">    返回一个组合的Consumer ，它按顺序执行此操作，然后是after操作。如果执行任一操作引发异常，则将其转发给组合操作的调用者。如果执行此操作引发异常，则不会执行after操作。</span></span><br><span class="line"><span class="comment">    参形：</span></span><br><span class="line"><span class="comment">    after – 此操作之后要执行的操作</span></span><br><span class="line"><span class="comment">    返回值：</span></span><br><span class="line"><span class="comment">    一个组合的Consumer ，它按顺序执行此操作，然后执行after操作</span></span><br><span class="line"><span class="comment">    抛出：NullPointerException – 如果after为 null</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">default</span> Consumer&lt;T&gt; <span class="title function_">andThen</span><span class="params">(Consumer&lt;? <span class="built_in">super</span> T&gt; after)</span> &#123;</span><br><span class="line">        Objects.requireNonNull(after);</span><br><span class="line">        <span class="keyword">return</span> (T t) -&gt; &#123; accept(t); after.accept(t); &#125;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="Runnable无参无返回型函数"><a href="#Runnable无参无返回型函数" class="headerlink" title="Runnable无参无返回型函数"></a>Runnable无参无返回型函数</h3><p><code>Runnable</code>的表现形式为即没有参数也没有返回值</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">Runnable接口应该由其实例打算由线程执行的任何类实现。该类必须定义一个名为run的无参数方法。</span></span><br><span class="line"><span class="comment">该接口旨在为希望在活动时执行代码的对象提供通用协议。例如， Runnable由类Thread实现。处于活动状态仅意味着线程已启动且尚未停止。</span></span><br><span class="line"><span class="comment">此外， Runnable提供了使类处于活动状态而不是子类化Thread的方法。实现Runnable的类可以通过实例化Thread实例并将其自身作为目标传入而无需子类化Thread即可运行。在大多数情况下，如果您只打算覆盖run()方法而不打算覆盖其他Thread方法，则应该使用Runnable接口。这很重要，因为除非程序员打算修改或增强类的基本行为，否则不应将类子类化。</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@FunctionalInterface</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title class_">Runnable</span> &#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">    当使用实现接口Runnable的对象创建线程时，启动线程会导致在该单独执行的线程中调用对象的run方法。</span></span><br><span class="line"><span class="comment">    方法run的一般约定是它可以采取任何行动。</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title function_">run</span><span class="params">()</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="Function函数"><a href="#Function函数" class="headerlink" title="Function函数"></a>Function函数</h3><p><code>Function</code>函数的表现形式为接收一个参数，并返回一个值。<code>Supplier</code>、<code>Consumer</code>和<code>Runnable</code>可以看作Function的一种特殊表现形式</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">表示接受一个参数并产生结果的函数。</span></span><br><span class="line"><span class="comment">这是一个功能接口，其功能方法是apply(Object) 。</span></span><br><span class="line"><span class="comment">自：</span></span><br><span class="line"><span class="comment">1.8</span></span><br><span class="line"><span class="comment">类型形参：</span></span><br><span class="line"><span class="comment">&lt; T &gt;——函数的输入类型</span></span><br><span class="line"><span class="comment">&lt; R &gt; – 函数结果的类型</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@FunctionalInterface</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title class_">Function</span>&lt;T, R&gt; &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">    将此函数应用于给定的参数。</span></span><br><span class="line"><span class="comment">    论据：</span></span><br><span class="line"><span class="comment">    t - 函数参数</span></span><br><span class="line"><span class="comment">    退货声明：</span></span><br><span class="line"><span class="comment">    函数结果</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    R <span class="title function_">apply</span><span class="params">(T t)</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     返回一个组合函数，该函数首先将before函数应用于其输入，然后将此函数应用于结果。如果任一函数的求值引发异常，则将其转发给组合函数的调用者。</span></span><br><span class="line"><span class="comment">        参形：</span></span><br><span class="line"><span class="comment">        before – 在应用此函数之前要应用的函数</span></span><br><span class="line"><span class="comment">        返回值：</span></span><br><span class="line"><span class="comment">        一个组合函数，它首先应用before函数，然后应用这个函数</span></span><br><span class="line"><span class="comment">        抛出：</span></span><br><span class="line"><span class="comment">        NullPointerException – 如果之前为空</span></span><br><span class="line"><span class="comment">        请参阅：</span></span><br><span class="line"><span class="comment">        andThen(Function)</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">default</span> &lt;V&gt; Function&lt;V, R&gt; <span class="title function_">compose</span><span class="params">(Function&lt;? <span class="built_in">super</span> V, ? extends T&gt; before)</span> &#123;</span><br><span class="line">        Objects.requireNonNull(before);</span><br><span class="line">        <span class="keyword">return</span> (V v) -&gt; apply(before.apply(v));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     返回一个组合函数，该函数首先将此函数应用于其输入，然后将after函数应用于结果。如果任一函数的求值引发异常，则将其转发给组合函数的调用者。</span></span><br><span class="line"><span class="comment">        参形：</span></span><br><span class="line"><span class="comment">        after – 应用此函数后要应用的函数</span></span><br><span class="line"><span class="comment">        返回值：</span></span><br><span class="line"><span class="comment">        一个组合函数，首先应用此函数，然后应用after函数</span></span><br><span class="line"><span class="comment">        抛出：</span></span><br><span class="line"><span class="comment">        NullPointerException – 如果 after 为 null</span></span><br><span class="line"><span class="comment">        请参阅：</span></span><br><span class="line"><span class="comment">        compose(Function)</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">default</span> &lt;V&gt; Function&lt;T, V&gt; <span class="title function_">andThen</span><span class="params">(Function&lt;? <span class="built_in">super</span> R, ? extends V&gt; after)</span> &#123;</span><br><span class="line">        Objects.requireNonNull(after);</span><br><span class="line">        <span class="keyword">return</span> (T t) -&gt; after.apply(apply(t));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">    返回一个始终返回其输入参数的函数。</span></span><br><span class="line"><span class="comment">    返回值：</span></span><br><span class="line"><span class="comment">    始终返回其输入参数的函数</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">static</span> &lt;T&gt; Function&lt;T, T&gt; <span class="title function_">identity</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> t -&gt; t;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="使用说明"><a href="#使用说明" class="headerlink" title="使用说明"></a>使用说明</h2><h3 id="异常信息处理"><a href="#异常信息处理" class="headerlink" title="异常信息处理"></a>异常信息处理</h3><blockquote>
<p>函数方法定义</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@FunctionalInterface</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title class_">ThrowExceptionFunction</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 抛出异常信息</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> supplier 异常信息提供者</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> void</span></span><br><span class="line"><span class="comment">     **/</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">throwMessage</span><span class="params">(Supplier supplier)</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>函数方法使用,如果传入为<code>true</code>则抛出一个异常，异常信息由<code>Supplier</code>提供</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> ThrowExceptionFunction <span class="title function_">isTrue</span><span class="params">(<span class="type">boolean</span> b)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> (supplier) -&gt; &#123;</span><br><span class="line">        <span class="keyword">if</span> (b) &#123;</span><br><span class="line">            <span class="type">String</span> <span class="variable">str</span> <span class="operator">=</span> (String) supplier.get();</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">RuntimeException</span>(str);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>使用演示</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">isTrue</span><span class="params">()</span> &#123;</span><br><span class="line">    VUtils.isTrue(<span class="literal">false</span>).throwMessage(() -&gt; <span class="string">&quot;有异常信息&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="分支处理"><a href="#分支处理" class="headerlink" title="分支处理"></a>分支处理</h3><blockquote>
<p>函数方法定义</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@FunctionalInterface</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title class_">BranchHandle</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 分歧终端机</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> trueHandler  为真处理器</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> falseHandler 为假处理器</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">trueOrFalseHandle</span><span class="params">(Runnable trueHandler, Runnable falseHandler)</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>函数方法使用，针对传入的true或false执行不同的处理逻辑</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> BranchHandle <span class="title function_">isTureOrFalse</span><span class="params">(<span class="type">boolean</span> b)</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> (trueHandler, falseHandler) -&gt; &#123;</span><br><span class="line">            <span class="keyword">if</span> (b) &#123;</span><br><span class="line">                trueHandler.run();</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                falseHandler.run();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>使用演示</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">isTureOrFalseTest</span><span class="params">()</span> &#123;</span><br><span class="line">    VUtils.isTureOrFalse(<span class="literal">false</span>).trueOrFalseHandle(() -&gt; &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;成功了&quot;</span>);</span><br><span class="line">    &#125;, () -&gt; &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;失败了&quot;</span>);</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="使用Comsumer实现的分支处理器"><a href="#使用Comsumer实现的分支处理器" class="headerlink" title="使用Comsumer实现的分支处理器"></a>使用Comsumer实现的分支处理器</h3><blockquote>
<p>函数方法定义</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@FunctionalInterface</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title class_">PresentOrElseHandler</span>&lt;T <span class="keyword">extends</span> <span class="title class_">Objects</span>&gt; &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 值不为空时执行消费操作</span></span><br><span class="line"><span class="comment">     * 值为空时执行其他的操作</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> action      值不为空时，执行的消费操作</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> emptyAction 值为空时，执行的操作</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> void</span></span><br><span class="line"><span class="comment">     **/</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">presentOrElseHandle</span><span class="params">(Consumer&lt;? <span class="built_in">super</span> T&gt; action, Runnable emptyAction)</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>函数方法使用，传入的字符串为空与不为空执行不同的逻辑</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> PresentOrElseHandler <span class="title function_">isBlankOrNoBlank</span><span class="params">(String str)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> (action, emptyAction) -&gt; &#123;</span><br><span class="line">        <span class="keyword">if</span> (str == <span class="literal">null</span> || str.length() == <span class="number">0</span>) &#123;</span><br><span class="line">            emptyAction.run();</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            action.accept(str);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>使用演示</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">isBlankOrNoBlankTest</span><span class="params">()</span> &#123;</span><br><span class="line">    VUtils.isBlankOrNoBlank(<span class="string">&quot;hello&quot;</span>).presentOrElseHandle(</span><br><span class="line">            System.out::println, () -&gt; &#123;</span><br><span class="line">                System.out.println(<span class="string">&quot;这是一个空字符串&quot;</span>);</span><br><span class="line">            &#125;</span><br><span class="line">    );</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">albafica</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://albafica.gitee.io/2022/05/07/functionalinterface/">https://albafica.gitee.io/2022/05/07/functionalinterface/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://albafica.gitee.io" target="_blank">若愚湖畔</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/hexoblog/tags/java/">java</a></div><div class="post_share"></div></div><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/hexoblog/2022/04/25/stream-api/" title="使用Stream API优化代码[转载]"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-04-25</div><div class="title">使用Stream API优化代码[转载]</div></div></a></div><div><a href="/hexoblog/2022/05/02/optional/" title="optional的使用"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-02</div><div class="title">optional的使用</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://s1.ax1x.com/2022/04/23/Lhpz3F.png" onerror="this.onerror=null;this.src='/hexoblog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">albafica</div><div class="author-info__description">Stay hungry Stay foolish</div></div><div class="card-info-data is-center"><div class="card-info-data-item"><a href="/hexoblog/archives/"><div class="headline">文章</div><div class="length-num">11</div></a></div><div class="card-info-data-item"><a href="/hexoblog/tags/"><div class="headline">标签</div><div class="length-num">6</div></a></div><div class="card-info-data-item"><a href="/hexoblog/categories/"><div class="headline">分类</div><div class="length-num">2</div></a></div></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Function-%E5%87%BD%E6%95%B0%E5%BC%8F%E6%8E%A5%E5%8F%A3"><span class="toc-number">1.</span> <span class="toc-text">Function 函数式接口</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E8%AF%B4%E6%98%8E"><span class="toc-number">1.1.</span> <span class="toc-text">函数说明</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Supplier%E4%BE%9B%E7%BB%99%E5%9E%8B%E5%87%BD%E6%95%B0"><span class="toc-number">1.1.1.</span> <span class="toc-text">Supplier供给型函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Consumer%E6%B6%88%E8%B4%B9%E5%9E%8B%E5%87%BD%E6%95%B0"><span class="toc-number">1.1.2.</span> <span class="toc-text">Consumer消费型函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Runnable%E6%97%A0%E5%8F%82%E6%97%A0%E8%BF%94%E5%9B%9E%E5%9E%8B%E5%87%BD%E6%95%B0"><span class="toc-number">1.1.3.</span> <span class="toc-text">Runnable无参无返回型函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Function%E5%87%BD%E6%95%B0"><span class="toc-number">1.1.4.</span> <span class="toc-text">Function函数</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E"><span class="toc-number">1.2.</span> <span class="toc-text">使用说明</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BC%82%E5%B8%B8%E4%BF%A1%E6%81%AF%E5%A4%84%E7%90%86"><span class="toc-number">1.2.1.</span> <span class="toc-text">异常信息处理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%86%E6%94%AF%E5%A4%84%E7%90%86"><span class="toc-number">1.2.2.</span> <span class="toc-text">分支处理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8Comsumer%E5%AE%9E%E7%8E%B0%E7%9A%84%E5%88%86%E6%94%AF%E5%A4%84%E7%90%86%E5%99%A8"><span class="toc-number">1.2.3.</span> <span class="toc-text">使用Comsumer实现的分支处理器</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/hexoblog/2022/05/18/techupdate/" title="技术升级简要构思">技术升级简要构思</a><time datetime="2022-05-18T12:54:43.000Z" title="发表于 2022-05-18 20:54:43">2022-05-18</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/hexoblog/2022/05/07/functionalinterface/" title="Function接口简单代码结构">Function接口简单代码结构</a><time datetime="2022-05-07T10:18:48.000Z" title="发表于 2022-05-07 18:18:48">2022-05-07</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/hexoblog/2022/05/07/cap/" title="微服务-CAP理论">微服务-CAP理论</a><time datetime="2022-05-07T01:12:07.000Z" title="发表于 2022-05-07 09:12:07">2022-05-07</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/hexoblog/2022/05/02/optional/" title="optional的使用">optional的使用</a><time datetime="2022-05-02T02:10:03.000Z" title="发表于 2022-05-02 10:10:03">2022-05-02</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/hexoblog/2022/05/01/pattern-overload/" title="设计模式-概述">设计模式-概述</a><time datetime="2022-05-01T10:12:20.000Z" title="发表于 2022-05-01 18:12:20">2022-05-01</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By albafica</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/hexoblog/js/utils.js"></script><script src="/hexoblog/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><div class="js-pjax"></div><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><script id="click-show-text" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-show-text.min.js" data-mobile="false" data-text="富强,民主,文明,和谐,爱国,敬业,诚信,友善" data-fontsize="15px" data-random="false" async="async"></script></div></body></html>